/**
 * Created by Android on 2017/12/13.
 */
import React, {Component} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Image,
    FlatList,
    ListView,
    ActivityIndicator,
    RefreshControl,
    TouchableOpacity
} from 'react-native';

import NavigationBar from "../component/NavigationBar"
import ProjectRow from "../component/ProjectRow"
import ScrollableTabView from "react-native-scrollable-tab-view"

//"最热"是包含在，HomePage页面
//"最热"页面包含，NavigationBar

export default class PopularPage extends Component{
    constructor(props){
        super(props);
        this.state={
            languages:["Android","IOS","Java","React","JS"]
        }
    }
    renderRightBtn =()=>{
        return <View style={{flexDirection:'row',alignItems:'center'}}>
            <TouchableOpacity
                activeOpacity={0.7}>
                <Image source={require('../../res/images/ic_search_white_48pt.png')} style={styles.navBtn}/>
            </TouchableOpacity>

            <TouchableOpacity
                activeOpacity={0.7}>
                <Image source={require('../../res/images/ic_more_vert_white_48pt.png')} style={styles.navBtn}/>
            </TouchableOpacity>
        </View>
    }

    render(){
        return <View style={styles.container}>
            <NavigationBar
                titles="热门"
                rightButton={this.renderRightBtn()}
            />
            <ScrollableTabView
                tabBarBackgroundColor="#63B8FF"
                tabBarActiveTextColor="#FFF"
                tabBarInactiveTextColor="#F5FFFA"
                tabBarUnderlineStyle={{backgroundColor:"#E7E7E7",height:2}}>
                {
                    this.state.languages.map((item,i)=>{
                        return <PopularTab key={`item${i}`} tabLabel={item}></PopularTab>
                    })
                }
            </ScrollableTabView>
        </View>;
    }


}
class  PopularTab extends Component{
    static defaultProps={
        tabLabel:"Android"
    }
    constructor(props){
        super(props)
        this.state={
            dataList:[],
            isLoading: true,
        }
    }
    loadData=()=>{
        fetch( `https://api.github.com/search/repositories?q=${this.props.tabLabel}&sort=stars`)
            .then((response) => response.json())
            .then(responseJson=>{
                let data = responseJson.items;
                let dataBlob = [];
                let i = 0;
                //放入 datablob 里面
                data.map((item,i)=>{
                     dataBlob.push({
                         key: i,
                         value: item,
                       });
                });

                this.setState({
                    //复制数据源
                    dataList: dataBlob,
                    isLoading:false
                });
                data = null;
                dataBlob = null;

            })
            .catch((error)=>{
                console.error(error);
            }).done();
    }
    componentDidMount =()=>{
        this.loadData()
     }

     render(){
        if(this.state.isLoading){
            return this.renderLoadingView();
        }else{
            return this.renderData();
        }
     }

    /**
     * 获取数据成功后显示
     * @returns {XML}
     *                          onRefresh={this.handleRefresh()}
     */
     renderData(){
         return(
             <View style={styles.container}>
                 {
                     <FlatList
                         data={this.state.dataList}
                         renderItem={this.renderItemView}
                     >
                     </FlatList>
                 }
             </View>
         )
     }

    /**
     * 返回 flatList 里面的字布局
     * @param item
     * @returns {XML}
     */
    renderItemView({item}) {
        return (
           <ProjectRow item={item}/>
        );
    }

    handleRefresh=()=>{
        this.loadData();
    }
    //加载等待的view
    renderLoadingView() {
        return (
            <View style={styles.container}>
               <ActivityIndicator
                    animating={this.state.isLoading?true:false}
                    style={[styles.gray, {height: 80}]}
                    color='red'
                    size="large"
                />

            </View>
        );
    }


}
const styles = StyleSheet.create({
    container: {
        flex:1
    },
    title: {
        fontSize: 15,
        color: 'blue',
    },
    content: {
        fontSize: 15,
        color: 'black',
    },
    navBtn:{
        width:24,
        height:24
    }
});